package com.woniu.woniuproject.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.woniuproject.entity.Account_log;
import com.woniu.woniuproject.entity.Funding_not_matched;
import com.woniu.woniuproject.exceptions.StatusException;
import com.woniu.woniuproject.mapper.Account_logMapper;
import com.woniu.woniuproject.mapper.Funding_not_matchedMapper;
import com.woniu.woniuproject.mapper.moneyCountMapper;
import com.woniu.woniuproject.vo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 功能描述:<br>
 * 〈〉
 *
 * @author EDZ
 * @create 2020/11/18
 * @since 1.0.0
 */
@Service
public class FundQueueService {
    @Autowired
    Funding_not_matchedMapper funding_not_matchedMapper;
    @Autowired
    moneyCountMapper moneyCountMapper;
    @Autowired
    Account_logMapper account_logMapper;

    public  PageInfo<Funding_not_matched> queryall(Page page, Funding_not_matched funding_not_matched) {
        PageHelper.startPage(page.getNowPage(),page.getMaxPage());
        List<Funding_not_matched> looseLabels = funding_not_matchedMapper.selectByAplay(funding_not_matched);
        PageInfo<Funding_not_matched> info = new PageInfo<>(looseLabels);
        return info;
    }

    @Transactional
    public String updateStatus(Funding_not_matched funding_not_matched) {
        //改状态
           int i=funding_not_matchedMapper.updateByPrimaryKey(funding_not_matched);
            if(i==0){
                throw new StatusException("状态修改失败");
            }
        //往公司账户加钱
        int k=moneyCountMapper.updateadd(funding_not_matched.getFnotmatchedmoney());
        if(k==0){
            throw new StatusException("加钱修改失败");
        }
        //往系统日志添加字段
        Double money=moneyCountMapper.selectmoney();
        Account_log account_log = new Account_log();
        account_log.setAuserid(funding_not_matched.getFinvestrecordid());
        account_log.setAmainaccountid(1);
        account_log.setAreceiveorpay("收款");
        String s = new Date().getTime() + UUID.randomUUID().toString().substring(0, 3);
        account_log.setAtransferserialno(s);
        account_log.setAdate(new Date());
        account_log.setAtype("官标");
        Double fnotmatchedmoney = funding_not_matched.getFnotmatchedmoney();
        account_log.setAamount(fnotmatchedmoney);
        account_log.setAbeforetradingmoney(money-fnotmatchedmoney);
        account_log.setAaftertradingmoney(money);
        int o=account_logMapper.sysinsert(account_log);
        if(o==0){
            throw new StatusException("添加日志失败");
        }

        return s;
    }

    public int updateStatus1(Funding_not_matched funding_not_matched) {
        return funding_not_matchedMapper.updateByKey(funding_not_matched);
    }

    public PageInfo<Funding_not_matched> queryall2(Page page, Funding_not_matched funding_not_matched) {
        PageHelper.startPage(page.getNowPage(),page.getMaxPage());
        List<Funding_not_matched> looseLabels = funding_not_matchedMapper.selectByAplay2(funding_not_matched);
        PageInfo<Funding_not_matched> info = new PageInfo<>(looseLabels);
        return info;
    }
}